package at.jku.se.fastsell.db4o.transactions;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import at.jku.se.fastsell.data.Auction;
import at.jku.se.fastsell.db4o.DatabaseException;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class GetAllAuctionsTransaction extends Transaction<List<Auction>> {

	@Override
	public List<Auction> run() throws DatabaseException {
		List<Auction> auctions = new LinkedList<Auction>();
//		
//		ObjectSet<Auction> result = db.queryByExample(Auction.class);
//		auctions.addAll(result);
//		
//		return auctions;		
		
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//	    Key key = KeyFactory.createKey("Auction", "Fastsell");
	    // Run an ancestor query to ensure we see the most up-to-date
	    // view of the Greetings belonging to the selected Guestbook.
	    Query query = new Query("Auction", key);
	    List<Entity> auctionEntities = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
	    
	    for (Entity auctionEntity : auctionEntities) {
	    	Auction auction = new Auction();
	    	auction.setTitle((String) auctionEntity.getProperty("title"));
	    	auction.setAuctionID((long) auctionEntity.getProperty("id"));
//	    	auction.setBidCount((int) auctionEntity.getProperty("bidCount"));
	    	auction.setDescription((String) auctionEntity.getProperty("description"));
	    	auction.setCurPrice((double) auctionEntity.getProperty("price"));
	    	auction.setEnd((Date) auctionEntity.getProperty("end"));
	    	auction.setStart((Date) auctionEntity.getProperty("start"));
	    	
	    	auctions.add(auction);
	    }
	    return auctions;
	}

}
